import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terminal_flutter_demo/pages/getxpages/controllers/user.dart';

class GetXLoginPage extends StatefulWidget {
  const GetXLoginPage({Key? key}) : super(key: key);

  @override
  _GetXLoginPageState createState() => _GetXLoginPageState();
}

class _GetXLoginPageState extends State<GetXLoginPage> {
  final userController = Get.put(UserController());

  late final userNameTextEditingController;

  late final passwordTextEditingController;

  @override
  void initState() {
    super.initState();
    userController.user.update((val) {
      val!.username = 'tikeyc';
      val.password = '123456';
    });
    userNameTextEditingController =
        TextEditingController(text: userController.user.value.username);

    passwordTextEditingController =
        TextEditingController(text: userController.user.value.password);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('login'),
      ),
      body: Center(
        child: Column(
          children: [
            Text('arguments id: ${Get.arguments?['id']}'),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Icon(Icons.account_box, color: Colors.green),
                SizedBox(
                  width: 200,
                  height: 30,
                  child: TextField(
                    controller: userNameTextEditingController,
                    decoration: InputDecoration(
                      // icon: Icon(Icons.account_box),
                      prefixIcon: const Icon(Icons.account_box),
                      suffixIcon: IconButton(
                        onPressed: () {
                          userNameTextEditingController.clear();
                        },
                        icon: const Icon(Icons.clear),
                      ),
                      hintText: '请输入用户名',
                      border: const OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(15))),
                    ),
                    onChanged: (value) {
                      userController.user.update((val) {
                        val?.username = value;
                      });
                    },
                  ),
                ),
              ],
            ),
            const SizedBox(height: 30),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Icon(Icons.password_rounded, color: Colors.green),
                SizedBox(
                  width: 200,
                  height: 30,
                  child: TextField(
                    controller: passwordTextEditingController,
                    obscureText: true, // 密码隐藏文本
                    keyboardType: TextInputType.visiblePassword,
                    decoration: InputDecoration(
                      // icon: Icon(Icons.password_rounded),
                      prefixIcon: const Icon(Icons.password_rounded),
                      suffixIcon: IconButton(
                        onPressed: () {
                          passwordTextEditingController.clear();
                        },
                        icon: const Icon(Icons.clear),
                      ),
                      hintText: '请输入密码',
                      border: const OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(15))),
                    ),
                    onChanged: (value) {
                      userController.user.update((val) {
                        val?.password = value;
                      });
                    },
                  ),
                ),
              ],
            ),
            const SizedBox(height: 100),
            Obx(() => ElevatedButton(
                  onPressed: userController.user.value.username.isEmpty ||
                          userController.user.value.password.isEmpty
                      ? null
                      : () {
                          // Get.back();
                          if (Get.arguments?['id'] == 456) {
                            userController.token.value =
                                'token: asdf445454asdfasd';
                            // userController.update();
                            userController.user.update((val) {
                              val?.token = 'user token: abc123def456';
                              val?.name = 'tikeyc';
                            });
                            Get.toNamed('/shop');
                          } else {
                            Get.offNamedUntil(
                              '/home',
                              (route) => false,
                              arguments: {'result': 666},
                            );
                          }
                        },
                  child: const Text('登录'),
                )),
          ],
        ),
      ),
    );
  }
}
